﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

namespace UdrinkWater_Project.Officer
{
    public partial class UnManu : System.Web.UI.Page
    {
         private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 DataTable dtInList = GetInList();
            ASPxGridView1.DataSource = dtInList;
            ASPxGridView1.DataBind();
            }
           
            
        }
        private DataTable GetInList()
        {
            string sqltext = @"SELECT DISTINCT TB_Manufacture.Manu_Date, TB_Manufacture.Act_Officer,
(TB_Employee.Prefix+' '+ TB_Employee.Firstname + ' ' +TB_Employee.Lastname) AS EmpName, TB_Manufacture.Status_Manufacture,
TB_Status_Manufacture.Status_Manufacture_Name FROM TB_Employee INNER JOIN TB_Manufacture ON TB_Employee.ID_Card = TB_Manufacture.Act_Officer INNER JOIN TB_Status_Manufacture ON TB_Manufacture.Status_Manufacture = TB_Status_Manufacture.Status_Manufactue_ID
    WHERE TB_Manufacture.Status_Manufacture = 1";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        protected void ASPxGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Select")
            {
                string Id = ChangeYear(e.CommandArgs.CommandArgument.ToString());
                Session["ManuDateID"] = Id;
                Response.Redirect("BillManu.aspx");

            }
        }
        private string ChangeYear(string InDate)
        {
            string day = "";
            string mount = "";
            string years = "";
            try
            {
                try
                {
                    string[] stemp = InDate.Split('-');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);


                }
                catch (Exception)
                {

                    string[] stemp = InDate.Split('/');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);

                }


                if (int.Parse(years) > 2300)
                {
                    years = (int.Parse(years) - 543).ToString();
                }
                return (years + "-" + mount + "-" + day);
            }
            catch (Exception)
            {

            }

            return (years + "-" + mount + "-" + day);
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
            {
                ASPxCheckBox Checkbox_Name = (ASPxCheckBox)ASPxGridView1.FindRowCellTemplateControl(i, (GridViewDataColumn)ASPxGridView1.Columns["colMyColumn"], "ASPxCheckBox1");
                if (Checkbox_Name.Checked == true)
                {
                    object Order_Date = ASPxGridView1.GetRowValues(i, "Manu_Date");
                    string Manu_Date = Convert.ToDateTime(Order_Date).ToString("yyyy-MM-dd HH:mm:ss.fff", new System.Globalization.CultureInfo("en-US"));
                    UpdateS(Manu_Date);
                    Checkbox_Name.Checked = false;
                }            
            }
            DataTable dtInList = GetInList();
            ASPxGridView1.DataSource = dtInList;
            ASPxGridView1.DataBind();
        }
        public void UpdateS(string ID)
        {
            string sqltext = @"UPDATE TB_Manufacture
                              SET     Status_Manufacture = 2
                              WHERE Manu_Date = '"+ID+"'";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
    }
    }
